package android.slc.db_objectbox.core;

import android.slc.db_objectbox.sys.service.CsConfigDaoService;
import android.slc.db_objectbox.sys.service.CsDictDataDaoService;
import android.slc.db_objectbox.sys.service.CsDictTypeDaoService;
import android.slc.db_objectbox.sys.service.CsStateDaoService;
import android.slc.db_objectbox.sys.service.DaoService;
import android.slc.db_objectbox.sys.service.imp.CsConfigDaoServiceImp;
import android.slc.db_objectbox.sys.service.imp.CsDictDataDaoServiceImp;
import android.slc.db_objectbox.sys.service.imp.CsDictTypeDaoServiceImp;
import android.slc.db_objectbox.sys.service.imp.CsStateDaoServiceImp;
import android.slc.db_objectbox.user.service.UserDaoService;
import android.slc.db_objectbox.user.service.imp.UserDaoServiceImp;

import java.util.concurrent.ConcurrentHashMap;

public class DaoMaster {
    private final static ConcurrentHashMap<Class, DaoService> serviceMap = new ConcurrentHashMap<>();

    /**
     * 获取服务
     *
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T extends DaoService> T getService(Class<T> tClass) {
        T service = (T) serviceMap.get(tClass);
        if (service == null) {
            synchronized (DaoMaster.class) {
                service = (T) serviceMap.get(tClass);
                if (service == null) {
                    if (tClass.equals(CsDictTypeDaoService.class)) {
                        service = (T) new CsDictTypeDaoServiceImp();
                    } else if (tClass.equals(CsDictDataDaoService.class)) {
                        service = (T) new CsDictDataDaoServiceImp();
                    } else if (tClass.equals(CsStateDaoService.class)) {
                        service = (T) new CsStateDaoServiceImp();
                    } else if (tClass.equals(CsConfigDaoService.class)) {
                        service = (T) new CsConfigDaoServiceImp();
                    } else if (tClass.equals(UserDaoService.class)) {
                        service = (T) new UserDaoServiceImp();
                    }
                    if (service != null) {
                        serviceMap.put(tClass, service);
                    }
                }
            }
        }
        return service;
    }

    public interface DaoElementListener<T> {
        void onRemove(int dataDiversityVersion, T data);

        void onPut(int dataDiversityVersion, T data);

        void onComplete();
    }
}
